package it.francescogabbrielli.android.solidalapp.actions;

import it.francescogabbrielli.android.solidalapp.Action;
import it.francescogabbrielli.android.solidalapp.EndpointsUtils;
import it.francescogabbrielli.android.solidalapp.file.ResponseFileActivity;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Request;
import it.francescogabbrielli.android.solidalapp.responseendpoint.Responseendpoint;
import it.francescogabbrielli.android.solidalapp.responseendpoint.model.Message;

/**
 * Send a chat message from response side
 */
public class GCMInsertResponseMessage extends Action {

	@Override
	public void execute() throws Exception {
		Request req = getModel(Request.class);
		Message msg = new Message();
		msg.setId(req.getId());
		msg.setDeviceId(req.getDeviceId());
		msg.setMessage(req.getMessage());
		setModel(msg);
		Responseendpoint endpoint = EndpointsUtils.buildEndpoint(Responseendpoint.class);
		Message ret = endpoint.insertMessage(msg).execute();
		setModel(ret);
	}
	
	@Override
	public void onError(String error) {
		refireActivity(ResponseFileActivity.class);
	}

	@Override
	public void onPostExecute() throws Exception {
		refireActivity(ResponseFileActivity.class);
	}

}
